package com.woniuxy.portal.controller;

import cn.hutool.json.JSONUtil;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.woniuxy.portal.common.Result;
import com.woniuxy.portal.common.constant.RedisConstant;
import com.woniuxy.portal.entity.Address;
import com.woniuxy.portal.entity.China;
import com.woniuxy.portal.model.vo.AddressVo;
import com.woniuxy.portal.service.IAddressService;
import com.woniuxy.portal.service.IChinaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 作者
 * @since 2023-09-15
 */
@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private IAddressService addressService;
    @Autowired
    private IChinaService chinaService;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/findAll")
    public Result findAll(@RequestHeader("Authorization") String authorization) {
        String token = authorization.replace("Bearer ", "");
        JWT jwt = JWTUtil.parseToken(token);
        Number userId = (Number) jwt.getPayload("userId");
        // 从token中获取登录用户id，根据用户id查询地址
        List<Address> list = addressService.findAddressByUserId(userId.intValue());
        // 转换： List<Address>  转换为  List<AddressVo>
        List<AddressVo> addressVos = list.stream().map(address -> parseAddress(address)).collect(Collectors.toList());
        return Result.ok(addressVos);
    }

    private AddressVo parseAddress(Address address) {
        return new AddressVo(
                address.getId(), address.getProvince() + address.getCity() +
                address.getArea() + address.getDetailAddress(), address.getIsDefault());
    }

    // 查询所有的省份
    @GetMapping("/getAllProvinces")
    public Result getAllProvinces() {
        List<China> list = null;
        // 1. 先判断Redis缓存中是否有省份的key
        if (!redisTemplate.hasKey(RedisConstant.PROVINCES)) {
            // 1.1 缓存中不存在，就查询数据库：SELECT * FROM t_china WHERE pid=0
            list = chinaService.list(
                    Wrappers.lambdaQuery(China.class)
                            .ne(China::getId, 0)
                            .eq(China::getPid, 0));
            // 1.2. 存储到Redis中: 通过Redis的set集合存储
            list.forEach(china -> {
                redisTemplate.opsForSet().add(RedisConstant.PROVINCES, JSONUtil.toJsonStr(china));
            });
        } else {
            // 2. 直接从Redis中获取
            Set<String> datas = redisTemplate.opsForSet().members(RedisConstant.PROVINCES);
            list = datas.stream().map(str->{
                // str 就是China对象的JSON字符串
                China china = JSONUtil.toBean(str,China.class);
                return china;
            }).collect(Collectors.toList());
        }

        return Result.ok(list);
    }

    // 根据省份查询所有的城市
    @GetMapping("/getAllCities")
    public Result getAllCities(Integer proviceId) {
        List<China> list = chinaService.getAllCities(proviceId);
        return Result.ok(list);
    }
    // 根据城市加载区域
    @GetMapping("/getAllAreas")
    public Result getAllAreas(Integer cityId) {
        List<China> list = chinaService.getAllAreas(cityId);
        return Result.ok(list);
    }
    // 保存地址
    @PostMapping("/save")
    public Result save(@RequestBody Address address,@RequestHeader("Authorization") String authorization) {
        String token = authorization.replace("Bearer ", "");
        JWT jwt = JWTUtil.parseToken(token);
        Number userId = (Number) jwt.getPayload("userId");

        String province = (String) redisTemplate.opsForHash().get(RedisConstant.CHINA_CACHE,address.getProvince());
        String city = (String) redisTemplate.opsForHash().get(RedisConstant.CHINA_CACHE,address.getCity());
        String area = (String) redisTemplate.opsForHash().get(RedisConstant.CHINA_CACHE,address.getArea());
        address.setProvince(province);
        address.setCity(city);
        address.setArea(area);
        address.setStatus("1");
        address.setUserId(userId.longValue());

        // 如果客户端传入的地址isDefault=1, 说明是默认地址。 那么其他的地址全部修改为0
        if ("1".equals(address.getIsDefault())) {
            // 修改其他的地址全部isDefault=0
            addressService.update(Wrappers.lambdaUpdate(Address.class).set(Address::getIsDefault,"0"));
        }

        // 保存地址
        addressService.save(address);
        return Result.ok();
    }
}














